/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Request;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class RequestDAO extends DAO {

    public RequestDAO() {
        super();
    }
    //BASIC DAO METHODS

    @SuppressWarnings("unchecked")
    public List<Request> getAll() throws Exception {
        List<Request> a = (List<Request>) getSession().createQuery("from Request a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Request get(int id) throws Exception {
        Request a = (Request) getSession().createQuery("from Request a where a.requestid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Request a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Request a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Request> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Request> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
//      USE get(int id) above..
//    @SuppressWarnings("unchecked")
//    public Request getById(int raid){
//        Request l=null;
//        try{
//            l = (Request) getSession().createQuery("from Request r where r.requestid=:raid")
//                    .setParameter("raid", raid)
//                    .setMaxResults(1)
//                    .uniqueResult();
//        }catch(Exception ex){
//            ex.printStackTrace();
//        }
//        return l;
//    }
    @SuppressWarnings("unchecked")
    public List<Request> getBranchid(int id) throws Exception {
        List<Request> requestlist = getSession().createQuery("from Request r where r.branches.branchid = :id").setParameter("id", id).setCacheable(true).list();
        flush();
        return requestlist;
    }
}
